#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    string decodeString(string s) {
        vector<string> str; str.push_back("");
        vector<int> nums;
        for (int i = 0; i < s.size();)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                int num = 0;
                while (i < s.size() && s[i] >= '0' && s[i] <= '9')
                {
                    num = num * 10 + (s[i++] - '0');
                }
                nums.push_back(num);
            }
            else if (s[i] == '[')
            {
                i++;
                string tmp;
                while (i < s.size() && s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i++];
                }
                str.push_back(tmp);
            }
            else if (s[i] == ']')
            {
                int num = nums.back(); nums.pop_back();
                string tmp = str.back(); str.pop_back();
                string tgt;
                while (num--) tgt += tmp;
                str.back() += tgt;
                i++;
            }
            else
            {
                string tmp;
                while (i < s.size() && s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i++];
                }
                str.back() += tmp;
            }
        }
        string ret;
        for (auto& e : str) ret += e;
        return ret;
    }
};